<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.INNERDEVICENAME"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="安装位置" prop="location">
        <el-input
          v-model="queryParams.DEVICENAME"
          placeholder="请输入安装位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域" prop="geoId">
        <el-select v-model="queryParams.geoId" placeholder="请选择安装区域">
          <el-option
            v-for="item in geometrys"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['app:inmvdevice:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['app:inmvdevice:remove']"
          >删除</el-button
        > -->
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:device:export']"
            >导出</el-button
          >
        </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="deviceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="设备名称" align="center" prop="INNERDEVICENAME" />
      <!-- <el-table-column label="设备类型" align="center" prop="type">
            <template slot-scope="scope">
                {{ examType[scope.row.type] }}
            </template>
        </el-table-column> -->
      <el-table-column label="x坐标" align="center" prop="SCRX" />
      <el-table-column label="y坐标" align="center" prop="SCRY" />
      <el-table-column label="区域" align="center" prop="geoId">
        <template slot-scope="scope">
          {{ euType[scope.row.geoId] }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="设备类型" align="center" prop="type" /> -->

      <!-- <el-table-column label="安装位置" align="center" prop="DEVICENAME" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:inmvdevice:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:inmvdevice:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.currentPage"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改摄像机位置点位以及链接对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="DEVICENAME">
          <el-input
            v-model="form.INNERDEVICENAME"
            placeholder="请输入设备名称"
          />
        </el-form-item>
        <el-form-item label="选择点位">
          <el-button type="info" @click="mapPoint">选择设备坐标</el-button>
        </el-form-item>

        <el-form-item label="x坐标" prop="SCRX">
          <el-input v-model="form.SCRX" placeholder="请输入x坐标" />
        </el-form-item>
        <el-form-item label="y坐标" prop="SCRY">
          <el-input v-model="form.SCRY" placeholder="请输入坐标" />
        </el-form-item>
        <!-- <el-form-item label="安装位置" prop="DEVICENAME">
            <el-input v-model="form.location" placeholder="请输入安装位置" />
          </el-form-item> -->
        <el-form-item label="安装区域" prop="geoId">
          <el-select v-model="form.geoId" placeholder="请选择安装区域">
            <el-option
              v-for="item in geometrys"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="SCRY">
          <!-- <el-input v-model="form.type" placeholder="请输入设备类型" /> -->
          <el-select v-model="form.type" placeholder="请选择设备类型">
            <el-option label="表面位移" :value="1"></el-option>
            <el-option label="内部位移" :value="2"></el-option>
            <el-option label="爆破振动" :value="3"></el-option>
            <!-- <el-option label="库水位" value="库水位"></el-option> -->
            <!-- <el-option label="表面位移" value="item.id"></el-option> -->
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="openMap" append-to-body>
      <div style="font-size: 20px">
        <span style="margin-right: 20px">X坐标:{{ form.SCRX }}</span
        ><span>Y坐标:{{ form.SCRY }}</span>
      </div>
      <div id="openMap" style="height:70vh"></div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="openMap = false">确 定</el-button>
        <el-button @click="openMap = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
    
  <script>
import { geometryList } from "@/api/system/video";
import {
  listEquipDevice,
  getDevice,
  addDevice,
  updateDevice,
  delDevice,
} from "@/api/system/broadcast";

import {
  surfdeviceGetLis,
  surfdeviceAdd,
  surfdeviceUpdate,
  surfdeviceGetAll,
  inmvdeviceGetLis,
  inmvdeviceAdd,
  inmvdeviceUpdate
} from "@/api/city";
import { bd09towgs84,wgs84tobd09 } from "@/utils/bub";
export default {
  name: "Device",
  data() {
    return {
      euType: {
        40: "采矿场",
        58: "尾矿库",
        56: "排土场",
      },
      //区域列表
      imgBalance: require("@/assets/s/position.png"),
      geometrys: [],
      // 遮罩层
      loading: true,
      openMap: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 摄像机位置点位以及链接表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        currentPage: 1,
        pageSize: 10,
        geoId: 40,
      },
      examType: {
        1: "wifi",
        2: "广播",
        3: "设备",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      Map: null,
    };
  },
  async created() {
    await this.getList();
    await geometryList().then((res) => {
      console.log(res, "resss");
      this.geometrys = res.data;
    });
  },
  methods: {
    mapPoint() {
      this.openMap = true;
      this.$nextTick(() => {
        this.Map = new BMap.Map("openMap", { mapType: BMAP_SATELLITE_MAP }); // 创建Map实例

        console.log(this.Map);
        this.Map.centerAndZoom(new BMap.Point(114.608347, 24.120331), 16); // 初始化地图,设置中心点坐标和地图级别
        this.Map.enableScrollWheelZoom(true); // 开启鼠标滚轮缩放

        let pit = new BMap.Point(wgs84tobd09(this.form.SCRX, this.form.SCRY)[0],wgs84tobd09(this.form.SCRX, this.form.SCRY)[1]);
          var Icon = new BMap.Icon(
            this.imgBalance,
            new BMap.Size(50, 50)
          );
          Icon.setImageSize(new BMap.Size(30, 30));
          var marker1 = new BMap.Marker(pit, {
            icon: Icon,
          });
          this.Map.addOverlay(marker1);
        this.Map.addEventListener("click", (e) => {
          // console.log(e.point.lng + "," + e.point.lat);
          this.Map.clearOverlays();
          this.form.SCRX = bd09towgs84(e.point.lng,e.point.lat)[0];
          this.form.SCRY = bd09towgs84(e.point.lng,e.point.lat)[1];
          let pitA = new BMap.Point(e.point.lng, e.point.lat);
          var myIcon = new BMap.Icon(
            this.imgBalance,
            new BMap.Size(30, 30)
          );
          myIcon.setImageSize(new BMap.Size(30, 30));
          var marker = new BMap.Marker(pitA, {
            icon: myIcon,
          });
          this.Map.addOverlay(marker);
        });
        
      });
    },
    /** 查询摄像机位置点位以及链接列表 */
    async getList() {
      this.loading = true;
      await inmvdeviceGetLis(this.queryParams).then((response) => {
        console.log(response);
        this.deviceList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        SECTIONID: null,
        DEVICENAME: null,
        SCRX: null,
        SCRY: null,
        type: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.currentPage = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备位置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const newForm = { ...row };

      this.form = newForm;
      const id = row.id || this.ids;
      this.open = true;
      this.title = "修改设备位置";

      // getDevice(id).then((response) => {
      // this.form = response.data;
      // this.open = true;
      // this.title = "修改设备位置";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (true) {
            this.form.SCRX = Number(this.form.SCRX);

            this.form.SCRY = Number(this.form.SCRY);

            inmvdeviceUpdate(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            surfdeviceAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(
          '是否确认删除广播位置点位以及链接编号为"' + ids + '"的数据项？'
        )
        .then(function () {
          return delDevice({ id: ids });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/device/export",
        {
          ...this.queryParams,
        },
        `device_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
  <style scoped lang="scss">
#openMap {
  width: 100%;
  height: 100vh;
}
</style>
    